<!DOCTYPE html>
<?php
$frist=!count($_POST);
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if($frist){
            if(isset($_GET["id"])){
                $uid=(int)$_GET["id"];
                if($uid){
                     $hostname="127.0.0.1";
                    $username="root";
                    $password="abc123";
                    $database="mybb_test";
                    $connect=mysqli_connect($hostname, $username, $password, $database);
                    if($connect){
            //            echo "数据库连接成功！";
                        //mysqli_set_charset 设置连接使用的字符集
                        mysqli_set_charset($connect, "utf-8");
                        $sqlstring="select * from user_infos where uid=${uid}";
                        $result=mysqli_query($connect, $sqlstring);
                        $row= mysqli_fetch_array($result);
//                        mysqli_num_rows 获取查询结果集合的数量
                        if(mysqli_num_rows($result)){
                            ?>
                             <h1>用户编辑</h1>
                        <form action="edituser.php" method="post">
                            <input type="hidden" name="uid" 
                                   value="<?php echo $row["uid"];?>" />
                        用户名：<input type="text" name="username" 
                                   value="<?php echo $row["username"];?>"/><br/>
                            密 &nbsp;&nbsp;码：<input type="password" name="upwd"
                                   value="<?php echo $row["upwd"];?>"/><br/>
                            年 &nbsp;&nbsp;龄：
                            <select name="age">
                                <?php
                                for($a=10;$a<100;$a++){
                                 if($a==$row["age"]){
                                 echo "<option selected=\"selected\">${a}</option>"; 
                                 }else{
                                     echo "<option>${a}</option>"; 
                                 }
                                }
                                ?>

                            </select>
                            <br/>
                            性 &nbsp;&nbsp;别：
                            <select name="sex">
                                <option <?php echo $row["sex"]=='男'?"selected=\"selected\"":""; ?>>男</option>
                                <option <?php echo $row["sex"]=='女'?"selected=\"selected\"":""; ?>>女</option>
                                <option <?php echo $row["sex"]=='保密'?"selected=\"selected\"":""; ?>>保密</option>
                            </select>
                            <br/>
                            <input type="submit" value="确定"/>
                            <input type="reset" value="取消" />
                        </form>
                            <?php
                        }else{
                            echo "参数错误";
                        }
                    }
                    mysqli_close($connect);
                }else{
                    echo "参数错误";
                }
            }else{
                echo "参数错误";
            }
           
        }else{
            //页面处理数据
            $name=$_POST["username"];
            $pwd=$_POST["upwd"];
            $sex=$_POST["sex"];
            $age=$_POST["age"];
            $user_id=$_POST["uid"];
            $hostname="127.0.0.1";
            $username="root";
            $password="abc123";
            $database="mybb_test";
            $connect=mysqli_connect($hostname, $username, $password, $database);
            if($connect){
                  mysqli_set_charset($connect, "utf-8");
                $sqlstring="update `user_infos` set `username`='${name}', `upwd`='${pwd}', "
                        . "`age`=${age}, `sex`='${sex}'"
                        . " where uid=${user_id}";
                           $result=mysqli_query($connect, $sqlstring);
                if($result){
                    echo "<script>alert('修改成功！');"
                        . "location='mysql001.php';</script>";
                }else{
                    echo "<script>alert('修改失败,请重试!!');"
                        . "location='mysql001.php';</script>";
                }
            }
            mysqli_close($connect);
        }
        ?>
        
        <?php
        // put your code here
        ?>
    </body>
</html>
